/**
 * created by 小卷毛, 2020/10/21
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.mdrh.task.viewmodel

import androidx.lifecycle.LifecycleOwner
import com.sgcc.pda.api.WSManager
import com.sgcc.pda.bluetooth.api.BlueManagerImpl
import com.sgcc.pda.frme.protocol.ws.sendbean.SafeSendParams
import com.sgcc.pda.mdrh.task.common_ui.event.BaseViewEvent
import com.sgcc.pda.mdrh.task.common_ui.helper.getTimeString
import com.sgcc.pda.mdrh.task.model.TxtConst.COMMAND_1
import com.sgcc.pda.mdrh.task.model.TxtConst.COMMAND_2
import com.sgcc.pda.mdrh.task.model.TxtConst.COMMAND_3
import com.sgcc.pda.mdrh.task.model.TxtConst.TXT_PATH
import com.sgcc.pda.tools.FileUtils
import com.sgcc.pda.tools.LogPrintUtils
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers

/**
 * 描 述：
 * 作 者：小卷毛  2020-10-21 18:05
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class MainViewModel : BaseViewEvent() {

    private val mDisposables by lazy { CompositeDisposable() }

    fun commandDevice(onResult: (String) -> Unit = {}) {
        mDisposables.add(
            Flowable.just(WSManager.getInstance())
                .subscribeOn(Schedulers.io())
                .map {
                    return@map it.safeIdentifyDevice
                }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    LogPrintUtils.i(it)

                    FileUtils.creatFolder(TXT_PATH)
                    FileUtils.creatFile(TXT_PATH + COMMAND_1)
                    FileUtils.writeFile(TXT_PATH + COMMAND_1, it)

                    onResult(it)
                }
        )
    }

    fun commandTask(
        num: String,
        work: String,
        type: String,
        onResult: (String) -> Unit = {}
    ) {
        mDisposables.add(
            Flowable.just(
                SafeSendParams().apply {
                    appNo = num
                    workType = when (work) {
                        "经互感器接入式低压电能计量装置装拆及验收" -> "0001"
                        "经互感器接入式低压电能计量装置故障处理" -> "0002"
                        "直接接入式电能计量装置装拆" -> "0003"
                        "直接接入式电能计量装置故障处理" -> "0004"
                        "高压电能计量装置装拆及验收" -> "0005"
                        "高压电能计量装置故障处理" -> "0006"
                        else -> "0007"
                    }
                    appType = type
                    createDate = System.currentTimeMillis().getTimeString("yyyyMMddHHmmss")
                }.toString()
            )
                .subscribeOn(Schedulers.io())
                .map {
                    return@map WSManager.getInstance().setSafeIdentifysTask(it)
                }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    LogPrintUtils.i(it)

                    FileUtils.creatFolder(TXT_PATH)
                    FileUtils.creatFile(TXT_PATH + COMMAND_2)
                    FileUtils.writeFile(TXT_PATH + COMMAND_2, it)

                    onResult(it)
                }
        )
    }

    fun commandSend(onResult: (String) -> Unit = {}) {
        mDisposables.add(
            Flowable.just(WSManager.getInstance())
                .subscribeOn(Schedulers.io())
                .map {
                    return@map it.setSafeIdentifySend()
                }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    LogPrintUtils.i(it)

                    FileUtils.creatFolder(TXT_PATH)
                    FileUtils.creatFile(TXT_PATH + COMMAND_3)
                    FileUtils.writeFile(TXT_PATH + COMMAND_3, it)

                    onResult(it)
                }
        )
    }

    override fun onDestroy(owner: LifecycleOwner) {
        super.onDestroy(owner)
        mDisposables.clear()
    }

}